package com.tsd.base.service.impl;

import com.tsd.base.dao.BaseExchangeRateMapper;
import com.tsd.base.entity.BaseExchangeRate;
import com.tsd.base.entity.BaseExchangeRateExt;
import com.tsd.base.service.BaseExchangeRateService;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.entity.SysUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 描述：BaseExchangeRate 服务实现层
 *
 * @author zsy
 * @date 2020/12/29 15:22:59
 */
@Service
public class BaseExchangeRateServiceImpl extends BaseServiceImpl implements BaseExchangeRateService {

    @Resource
    private BaseExchangeRateMapper baseExchangeRateMapper;

    @Override
    public BaseExchangeRate getBaseExchangeRateById(Long id) throws Exception {
        return baseExchangeRateMapper.selectByPrimaryKey(id);
    }

    @Override
    public BaseExchangeRateExt queryBySid(String sid) throws Exception {
        return baseExchangeRateMapper.selectBySid(sid);
    }

    @Override
    public void deleteBaseExchangeRate(Long id, SysUser opUser) throws Exception {
        BaseExchangeRate old = baseExchangeRateMapper.selectByPrimaryKey(id);
        super.checkEmpty(old, "该记录不存在");
        super.autoInjectBaseData(old, opUser, this.TYPE_DELETE);
        baseExchangeRateMapper.updateByPrimaryKeySelective(old);
    }

    @Override
    public void batchDelBaseExchangeRate(List<Long> ids, SysUser opUser) throws Exception {
        for (Long id : ids) {
            BaseExchangeRate old = baseExchangeRateMapper.selectByPrimaryKey(id);
            super.autoInjectBaseData(old, opUser, this.TYPE_DELETE);
            baseExchangeRateMapper.updateByPrimaryKeySelective(old);
        }
    }

    @Override
    public void saveBaseExchangeRate(BaseExchangeRate record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            super.autoInjectBaseData(record, opUser, this.TYPE_CREATE);
            baseExchangeRateMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, this.TYPE_UPDATE);
            baseExchangeRateMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public void batchSaveBaseExchangeRate(List<BaseExchangeRateExt> baseExchangeRateList, SysUser opUser) throws Exception {
        Integer defaultNum = 0;
        Boolean errorFlag = false;
        Boolean sameTypeFlag = false;
        for (BaseExchangeRateExt rate : baseExchangeRateList) {
            if (rate.getIs_default().equals(BaseExchangeRateExt.IS_DEFAULT)) {
                defaultNum++;
            }
            if (HlpUtils.isEmpty(rate.getCurrency_type())) {
                errorFlag = true;
            } else {
                List<BaseExchangeRateExt> filter = ListUtil.filter(baseExchangeRateList, t -> rate.getCurrency_type().equals(t.getCurrency_type()));
                if (!HlpUtils.isEmptyList(filter) && filter.size() > 1) {
                    sameTypeFlag = true;
                }
            }
        }
        if (defaultNum > 1) {
            throw new HlpException("只能选择一个默认币种");
        }
        if (errorFlag) {
            throw new HlpException("缺少外币信息");
        }
        if (sameTypeFlag) {
            throw new HlpException("存在重复外币,请调整");
        }
        if (defaultNum.equals(0)) {
            throw new HlpException("请选择一个默认币种");
        }
        for (BaseExchangeRate rate : baseExchangeRateList) {
            saveBaseExchangeRate(rate, opUser);
        }
    }

    @Override
    public List<BaseExchangeRateExt> findBaseExchangeRates(Map<String, Object> params, PageBean page) throws Exception {
        return baseExchangeRateMapper.selectByPage(params, page);
    }

    @Override
    public List<BaseExchangeRateExt> findBaseExchangeRates(Map<String, Object> params) throws Exception {
        return baseExchangeRateMapper.selectByParams(params);
    }

    @Override
    public BaseExchangeRate getDefaultBaseExchangeRate() throws Exception {
        BaseExchangeRate search = new BaseExchangeRate();
        search.setDr(0);
        search.setIs_default(1);
        List<BaseExchangeRateExt> result = baseExchangeRateMapper.selectByEntity(search);
        if (HlpUtils.isEmptyList(result)) {
            throw new HlpException("不存在默认汇率");
        }
        return result.get(0);
    }
}
